package com.orange.core.service.shopCategory.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.orange.core.service.shopCategory.ShopCategoryService;
import com.orange.core.mapper.shopCategory.ShopCategoryMapper;
import com.orange.core.pojo.PageReqDto;
import com.orange.core.pojo.entity.shopCategory.ShopCategory;
import com.orange.core.pojo.entity.shopCategory.dto.ShopCategoryDTO;
import com.orange.core.pojo.entity.shopCategory.vo.ShopCategoryVO;
import com.orange.core.pojo.req.shopCategory.ShopCategoryReq;
import com.orange.core.util.ConvertUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>ClassName:ShopCategoryImpl</p>
 * <p>Description: 楼层分类serviceImpl</p>
 * <p>Author: lwm</p>
 * <p>Date: 2025/5/24 15:40</p>
 */
@Service
public class ShopCategoryServiceImpl extends ServiceImpl<ShopCategoryMapper, ShopCategory>
        implements ShopCategoryService {


    @Override
    public Page<ShopCategoryVO> page(PageReqDto<ShopCategoryReq> pageReqDto) {
        String categoryName = Optional.ofNullable(pageReqDto.getCondition()).map(ShopCategoryReq::getCategoryName).orElse(null);
        Page<ShopCategory> page = this.lambdaQuery().like(StrUtil.isNotBlank(categoryName), ShopCategory::getCategoryName, categoryName)
                .orderByAsc(ShopCategory::getSortOrder)
                .page(pageReqDto.toPage());
        Page<ShopCategoryVO> shopCategoryVOPage = new Page<>(pageReqDto.getPage(), pageReqDto.getSize(), page.getTotal());
//        List<ShopCategoryVO> shopCategoryVOList = ConvertUtils.convertListToVo(page.getRecords(), ShopCategoryVO.class);
        List<ShopCategoryVO> shopCategoryList = page.getRecords().stream().map(shopCategory -> {
            ShopCategoryVO shopCategoryVO = new ShopCategoryVO();
            BeanUtil.copyProperties(shopCategory, shopCategoryVO);
//            Optional.ofNullable(shopCategory.getPointsRatio()).ifPresent(ration -> shopCategoryVO.setPointsRatio(ration * 100));
            return shopCategoryVO;
        }).collect(Collectors.toList());
        shopCategoryVOPage.setRecords(shopCategoryList);
        return shopCategoryVOPage;
    }


    @Override
    public ShopCategoryVO info(Integer id) {
        ShopCategory shopCategory = this.getById(id);
        Assert.notNull(shopCategory, "分类不存在");
        Float pointsRatio = shopCategory.getPointsRatio();
        ShopCategoryVO shopCategoryVO = new ShopCategoryVO();
        BeanUtil.copyProperties(shopCategory, shopCategoryVO);
//        Optional.ofNullable(pointsRatio).ifPresent(ration -> shopCategoryVO.setPointsRatio(ration * 100));
        return shopCategoryVO;
    }

    @Override
    @Transactional
    public boolean save(ShopCategoryDTO shopCategoryDTO) {
        ShopCategory shopCategory = new ShopCategory();
        BeanUtil.copyProperties(shopCategoryDTO, shopCategory);
//        Optional.ofNullable(shopCategoryDTO.getPointsRatio()).ifPresent(ration -> shopCategory.setPointsRatio(ration / 100));
        boolean result = this.save(shopCategory);
        return result;
    }

    @Override
    @Transactional
    public boolean update(ShopCategoryDTO shopCategoryDTO) {
        ShopCategory shopCategory = new ShopCategory();
        BeanUtil.copyProperties(shopCategoryDTO, shopCategory);
//        Optional.ofNullable(shopCategoryDTO.getPointsRatio()).ifPresent(ration -> shopCategory.setPointsRatio(ration / 100));
        boolean result = this.updateById(shopCategory);
        return result;
    }

    @Override
    @Transactional
    public boolean deleteShopCategoryByIds(Integer[] ids) {
        boolean result = this.removeBatchByIds(Arrays.asList(ids));
        return result;
    }

    @Override
    public List<ShopCategoryVO> shopCategoryList() {
        return Optional.ofNullable(this.list())
                .filter(CollUtil::isNotEmpty)
                .map(list -> ConvertUtils.convertListToVo(list, ShopCategoryVO.class))
                .orElse(List.of());
    }
}
